package com.sui.bigdata.flink.sql.sink.message;

import com.sui.bigdata.flink.sql.core.sink.IStreamSinkGener;
import com.sui.bigdata.flink.sql.core.table.TargetTableInfo;
import com.sui.bigdata.flink.sql.sink.message.table.MessageSinkTableInfo;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.typeutils.RowTypeInfo;
import org.apache.flink.api.java.typeutils.TupleTypeInfo;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSink;
import org.apache.flink.streaming.api.functions.sink.OutputFormatSinkFunction;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import org.apache.flink.table.sinks.RetractStreamTableSink;
import org.apache.flink.table.sinks.TableSink;
import org.apache.flink.types.Row;

/**
 * @author YongChen
 * @date 2020/3/3 10:55
 * @description
 * @email yong_chen@sui.com
 */
public class MessageSink implements RetractStreamTableSink<Row>, IStreamSinkGener<MessageSink> {

    protected String[] fieldNames;

    protected TypeInformation<?>[] fieldTypes;

    private String phone;

    private String template;

    @Override
    public TupleTypeInfo<Tuple2<Boolean, Row>> getOutputType() {
        return new TupleTypeInfo(org.apache.flink.table.api.Types.BOOLEAN(), getRecordType());
    }


    @Override
    public MessageSink genStreamSink(TargetTableInfo targetTableInfo) {

        MessageSinkTableInfo messageSinkTableInfo = (MessageSinkTableInfo) targetTableInfo;
        this.phone = messageSinkTableInfo.getPhone();
        this.template = messageSinkTableInfo.getTemplate();
        return this;
    }

    @Override
    public DataStreamSink<?> consumeDataStream(DataStream<Tuple2<Boolean, Row>> dataStream) {
        MessageOutputFormat.MessageOutputFormatBuilder builder = MessageOutputFormat.buildMessageOutputFormat();
        builder.setPhone(this.phone)
                .setTemplate(this.template)
                .setFieldNames(this.fieldNames)
                .setFieldTypes(this.fieldTypes);

        MessageOutputFormat messageOutputFormat = builder.finish();
        RichSinkFunction richSinkFunction = new OutputFormatSinkFunction(messageOutputFormat);
        return dataStream.addSink(richSinkFunction);
    }

    @Override
    public void emitDataStream(DataStream<Tuple2<Boolean, Row>> dataStream) {

    }

    @Override
    public TypeInformation<?>[] getFieldTypes() {
        return fieldTypes;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public String[] getFieldNames() {
        return fieldNames;
    }

    @Override
    public TypeInformation<Row> getRecordType() {
        return new RowTypeInfo(fieldTypes, fieldNames);
    }


    @Override
    public TableSink<Tuple2<Boolean, Row>> configure(String[] fieldNames, TypeInformation<?>[] fieldTypes) {
        this.fieldNames = fieldNames;
        this.fieldTypes = fieldTypes;
        return this;
    }
}
